package swingxrcp.framework.client.action;

import java.io.IOException;
import java.io.Serializable;

import swingxrcp.framework.model.Action;
import swingxrcp.framework.model.exception.NetworkException;
import swingxrcp.framework.util.log.Logger;
import swingxrcp.framework.util.log.LoggerFactory;

/**
 * Action客户端代理
 *
 * @author liangfei
 *
 */
public class ActionProxy implements Action {

	protected static Logger logger = LoggerFactory.getLogger(ActionProxy.class);

	protected final Transporter transporter;

	protected final String actionName;

	public ActionProxy(Transporter transporter, String actionName) {
		this.transporter = transporter;
		this.actionName = actionName;
	}

	public Serializable execute(Serializable object) {
		try {
			Serializable result = transporter.transport(actionName, object);
			if (result instanceof Throwable) {
				Throwable exception = (Throwable)result;
				logger.error(exception.getMessage(), new RuntimeException());
				if (exception instanceof RuntimeException)
					throw (RuntimeException)result;
				throw new ServerSideException(exception);
			}
			return result;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			throw new NetworkException(e.getMessage(), e);
		}
	}

}
